package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.bwie.domain.Order;
import com.bwie.service.OrderService;
import com.bwie.service.ProductService;
import com.bwie.service.SeckillService;
import com.bwie.service.WeChatPayService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * TODO
 *
 * @Description
 * @Author 36167
 * @Date 2025/3/8 11:26
 **/


@RestController
@RequestMapping("/prod")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    WeChatPayService weChatPayService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    SeckillService seckillService;

    @GetMapping("/prodelist")
    public R prodelist(PageInfoVo pageInfoVo){
        return productService.prodlist(pageInfoVo);
    }

    @PostMapping("/pay")
    public R pay(){
        String appid = "wxab8acb865bb1637e";
        String body = "尚品商云";
        String mchId = "11473623";
        String nonceStr = RandomUtil.randomString(10);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("appid",appid);
        hashMap.put("mch_id",mchId);
        hashMap.put("nonce_str",nonceStr);
        hashMap.put("body",body);
        hashMap.put("out_trade_no","DDP123456789");
        hashMap.put("total_fee","1");
        hashMap.put("spbill_create_ip","127.0.0.1");
        hashMap.put("notify_url","https://yourdomain.com/notify");
        hashMap.put("trade_type","NATIVE");

        String sign = weChatPayService.createSign(hashMap);
        hashMap.put("sign",sign);
        String pay = weChatPayService.createPay(hashMap);
        return R.OK(pay);
    }

    @PostMapping("/orderadd")
    public R orderadd(@RequestBody Order order){
       return orderService.orderadd(order);
    }


    @GetMapping("/orderlist")
    public R orderlist(){
       return orderService.orderlist();
    }

    //秒杀接口
   @PostMapping("/seckill")
    public R seckill(@RequestParam String pid,@RequestParam String uid){
        return productService.sckill(pid,uid);
   }

   @GetMapping("/secklist")
    public R secklist(){
        return seckillService.list1();
   }

   @GetMapping("/secklist1")
    public R secklist1(){
        return seckillService.list2();
   }

}
